package nape.dynamics;
import cx.FastList;
import cx.Allocator;
import cx.Algorithm;
import nape.dynamics.ObjArb;
import nape.dynamics.Contact;
import nape.phys.PhysObj;
import nape.phys.Body;
import nape.geom.Vec2;
import nape.geom.VecMath;




















class Arbiter {
	public var id:Int;
	
	
	public var obj_arb:ObjArb;
	public var ignore:Bool;
	public var temp_ignore:Bool;
	public var sensor:Bool;
	
	
	
	public var dyn_fric :Float;
	public var stat_fric:Float;
	public var restitution:Float;
	
	
	
	
	public var stamp:Int;
	public var updated:Bool;
	public var alloc:Allocator;
	
	
	
	
	public var contacts:CxFastList(Contact);
	public var p1:PhysObj; public var b1:Body; 
	public var p2:PhysObj; public var b2:Body;
	
	
	
	
	public var live:Bool;
	public var woken:Bool;
	public var sstamp:Int;
	
	
	
	public function new () {
		contacts = new CxFastList(Contact)();
	}
	
	
	
	
	public inline function getObjectA():Dynamic return untyped this._s1
	public inline function getObjectB():Dynamic return untyped this._s2
	
	
	
	public inline function impactImpulse():Vec2 {
		var ret = new Vec2();
		CxIter(c, contacts, {
			if(!c.fresh) cxcontinue;
			
			var jnAcc = c.jnAcc + c.pjnAcc;
			vec_addeq(ret.p, c.n, jnAcc);
		});
		return ret;
	}
	
	public inline function totalImpulse():Vec2 {
		var ret = new Vec2();
		CxIter(c, contacts, {
			var jnAcc = c.jnAcc + c.pjnAcc;
			vec_addeq(ret.p, c.n, jnAcc);
		});
		return ret;
	}
	
	public inline function totalImpulseWithFriction():Vec2 {
		var ret = new Vec2();
		CxIter(c, contacts, {
			vec_new(j);
			jx = c.jtAcc + c.pjtAcc;
			jy = c.jnAcc + c.pjnAcc;
			ret.px += vec_cross(j,c.n);
			ret.py += vec_dot  (j,c.n);
		});
		return ret;
	}
	
}